package bawei.com.adapter;


import android.content.Context;


import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;



import android.text.SpannableStringBuilder;



import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;


import android.widget.ImageView;


import android.widget.TextView;



import com.bumptech.glide.Glide;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.youth.banner.Banner;


import com.youth.banner.BannerConfig;


import java.util.ArrayList;
import java.util.List;



import bawei.com.bilibili.R;


import bawei.com.model.banner.GlideImageLoader;
import bawei.com.model.bean.SopCastBean;



import bawei.com.widget.CircleImageView;
import butterknife.BindView;


import butterknife.ButterKnife;

/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/10 14:20
 * 邮箱:18310832074@163.com
 */

public class SopCastAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


    private SopCastBean.DataBean dataBeen;

    private Context context;
    private int[] entranceIconRes = new int[] {
            R.drawable.live_home_follow_anchor,
            R.drawable.live_home_live_center,
            R.drawable.live_home_search_room,
            R.drawable.live_home_all_category
    };
    private String[] entranceTitles = new String[] {
            "关注", "中心",
            "搜索", "分类"
    };

    private static final int Banner1=0;
    private static final int Entrance=1;
    private static final int partition_title=2;
    private static final int partition=3;
    private int entranceSize= 4;
    public SopCastAdapter(Context context, SopCastBean.DataBean dataBeen) {
        this.context = context;
        this.dataBeen = dataBeen;
    }

    public int getSpanSize(int pos){

        int i = getItemViewType(pos);
        switch (i){

            case Entrance:
                return 3;
            case partition:
                return 6;
            case partition_title:
                return 12;
            case Banner1:
                return 12;

        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType){

            case Banner1:
                View  view= LayoutInflater.from(context).inflate(R.layout.item_live_banner,parent,false);
                Item_live_banner item_live_banner = new Item_live_banner(view);
                return item_live_banner;
            case Entrance:
                View   view1= LayoutInflater.from(context).inflate(R.layout.item_live_entrance,parent,false);
                Item_live_entrance item_live_entrance = new Item_live_entrance(view1);
                return item_live_entrance;
            case partition_title:
                View   view2= LayoutInflater.from(context).inflate(R.layout.item_live_partition_title,parent,false);
                Item_live_partition_title item_live_partition_title = new Item_live_partition_title(view2);
                return item_live_partition_title;
            case partition:
                View   view3= LayoutInflater.from(context).inflate(R.layout.item_live_partition,parent,false);
                Item_live_partition item_live_partition1 = new Item_live_partition(view3);
                return item_live_partition1;

        }


        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {



      if(getItemViewType(position)==0){

          List<SopCastBean.DataBean.BannerBean> banner = dataBeen.getBanner();
          List<String> stringList=new ArrayList<>();
          stringList.clear();
          for (int i = 0; i < banner.size(); i++) {
              stringList.add(banner.get(i).getImg());
          }
          ((Item_live_banner) holder).banner.setImageLoader(new GlideImageLoader())
                  .setImages(stringList)
                  .setBannerStyle(BannerConfig.RIGHT)
                  .setBannerStyle(BannerConfig.NUM_INDICATOR)
                  .start();

      }else
      if(getItemViewType(position)==1){

          int h=0;
          if(position==1){
              h=0;
          }else
              if(position==2){
                  h=1;
              }else
              if(position==3){
                  h=2;
              }else
              if(position==4){
                  h=3;
              }
          Glide.with(context)
                  .load(entranceIconRes[h])
                  .diskCacheStrategy(DiskCacheStrategy.ALL)
                  .placeholder(R.drawable.bili_default_image_tv)
                  .into(((Item_live_entrance) holder).imageView);
          ((Item_live_entrance) holder).textView.setText(entranceTitles[h]);
      }else
          if(getItemViewType(position)==2){

              Glide.with(context)
                      .load(dataBeen.getPartitions().get(getItemPosition(position)).getPartition().getSub_icon().getSrc())
                      .diskCacheStrategy(DiskCacheStrategy.ALL)
                      .placeholder(R.drawable.bili_default_image_tv)
                      .into(((Item_live_partition_title) holder).item_live_partition_icon);
              ((Item_live_partition_title) holder).item_live_partition_title.setText(dataBeen.getPartitions().get(getItemPosition(position)).getPartition().getName());
              SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
                      "当前" + dataBeen.getPartitions().get(getItemPosition(position)).getPartition().getCount() + "个直播");
              ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(
                      context.getResources().getColor(R.color.pink_text_color));
              stringBuilder.setSpan(foregroundColorSpan, 2,
                      stringBuilder.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
              ((Item_live_partition_title) holder).item_live_partition_count.setText(stringBuilder);

          }else

              if(getItemViewType(position)==3){
                  position-=1;
                  int i = getItemPosition(position) * 5;
                  int i1 = position - 1 - entranceSize - i;
                 // Log.i("TAG1", "onBindViewHolder:----- "+i1);
                  SopCastBean.DataBean.PartitionsBean.LivesBeanX livesBeanX =
                          dataBeen.getPartitions().get(getItemPosition(position))
                                  .getLives().get(i1);
                  ((Item_live_partition) holder).item_live_title.setText(livesBeanX.getTitle());
                  Glide.with(context)
                          .load(livesBeanX.getCover().getSrc())
                          .diskCacheStrategy(DiskCacheStrategy.ALL)
                          .placeholder(R.drawable.bili_default_image_tv)
                          .into(((Item_live_partition) holder).item_live_cover);
                  Glide.with(context)
                          .load(livesBeanX.getCover().getSrc())
                          .diskCacheStrategy(DiskCacheStrategy.ALL)
                          .placeholder(R.drawable.bili_default_image_tv)
                          .into(((Item_live_partition) holder).item_live_user_cover);
                  ((Item_live_partition) holder).item_live_title.setText(livesBeanX.getTitle());
                  ((Item_live_partition) holder).item_live_user.setText(livesBeanX.getOwner().getName());
                  ((Item_live_partition) holder).item_live_count.setText(livesBeanX.getOnline()+"");
              }

    }

    @Override
    public int getItemCount() {

        return 1+entranceTitles.length+dataBeen.getPartitions().size()*5;
    }

    /**
     * 获取当前Item在第几组中
     */
    private int getItemPosition(int pos) {

        pos -= entranceSize;
        return pos / 5;
    }
    private boolean isPartitionTitle(int pos) {

        pos -= entranceSize;

        return (pos % 5 == 0);
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return Banner1;
        }
        position -= 1;
        if (position < entranceSize) {
            return Entrance;
        } else if (isPartitionTitle(position)) {
            return partition_title;
        } else {
            return partition;
        }
    }
    //直播 轮播Banner
    class Item_live_banner extends RecyclerView.ViewHolder{
        Banner banner;
        public Item_live_banner(View itemView) {
            super(itemView);
            banner= (Banner) itemView.findViewById(R.id.banner);

        }
    }

    //直播分类
    class Item_live_entrance extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView textView;

        public Item_live_entrance(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.live_entrance_image);
            textView= (TextView) itemView.findViewById(R.id.live_entrance_title);
        }
    }

    //grid 类型item
    class Item_live_partition extends RecyclerView.ViewHolder{


        CardView itemLiveLayout;
        ImageView item_live_cover;
        CircleImageView item_live_user_cover;
        TextView item_live_title;
        TextView item_live_user;
        TextView item_live_count;

        public Item_live_partition(View itemView) {
            super(itemView);
            itemLiveLayout= (CardView) itemView.findViewById(R.id.item_live_layout);
            item_live_cover= (ImageView) itemView.findViewById(R.id.item_live_cover);
            item_live_user_cover= (CircleImageView) itemView.findViewById(R.id.item_live_user_cover);
            item_live_title= (TextView) itemView.findViewById(R.id.item_live_title);
            item_live_user= (TextView) itemView.findViewById(R.id.item_live_user);
            item_live_count= (TextView) itemView.findViewById(R.id.item_live_count);

        }
    }

    //item标题
    class Item_live_partition_title extends RecyclerView.ViewHolder{

        ImageView item_live_partition_icon;
        TextView item_live_partition_title;
        TextView item_live_partition_count;


        public Item_live_partition_title(View itemView) {
            super(itemView);
            item_live_partition_icon= (ImageView) itemView.findViewById(R.id.item_live_partition_icon);
            item_live_partition_title= (TextView) itemView.findViewById(R.id.item_live_partition_title);
            item_live_partition_count= (TextView) itemView.findViewById(R.id.item_live_partition_count);
        }
    }


}
